package logic

import (
	"context"
	"pro-train-hub/models/mysql"

	"pro-train-hub/trainrpc/internal/svc"
	"pro-train-hub/trainrpc/trainrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectListLogic {
	return &CollectListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CollectListLogic) CollectList(in *trainrpc.CollectListRequest) (*trainrpc.CollectListResponse, error) {
	// todo: add your logic here and delete this line
	var collect []mysql.CollectData
	mysql.DB.Debug().
		Model(&mysql.Collect{}).
		Select("users.username,lessons.title,users.avatar").
		Joins("left join users on users.id = collects.user_id").
		Joins("left join lessons on lessons.id = collects.lesson_id").
		Where("users.id = ?", in.UserId).
		Find(&collect)

	var listData []*trainrpc.CollectList
	for _, data := range collect {
		list := trainrpc.CollectList{
			Username: data.Username,
			Title:    data.Title,
			Avatar:   data.Avatar,
		}
		listData = append(listData, &list)
	}
	return &trainrpc.CollectListResponse{
		List: listData,
	}, nil
}
